﻿using System;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class User_Controls_MCalender : System.Web.UI.UserControl
{
    public string TargetControlId { get; set; }
    public string MinDate { get; set; }
    public string MaxDate { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(TargetControlId))
        {
            if (this.Parent.FindControl(TargetControlId)==null || this.Parent.FindControl(TargetControlId).GetType()!=typeof(TextBox))
            {
                LblErrorMsg.Text = "Invalid target control!";
                ImgBtnCal.Enabled = false;
            }
        }
    }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        try
        {
            if (!string.IsNullOrEmpty(MinDate))
            {
                string[] arrMinDate = MinDate.Split('/');
                string minDate = arrMinDate[1] + "/" + arrMinDate[0] + "/" + arrMinDate[2];
                if (e.Day.Date < Convert.ToDateTime(minDate))
                {
                    e.Day.IsSelectable = false;
                }
            }

            if (!string.IsNullOrEmpty(MaxDate))
            {
                string[] arrMaxDate = MaxDate.Split('/');
                string maxDate = arrMaxDate[1] + "/" + arrMaxDate[0] + "/" + arrMaxDate[2];
                if (e.Day.Date > Convert.ToDateTime(maxDate))
                {
                    e.Day.IsSelectable = false;
                }
            }
        }
        catch (Exception ex)
        {
            LblErrorMsg.Text = ex.Message;
        }
    }
    protected void ImgBtnCal_Click(object sender, ImageClickEventArgs e)
    {
        Calendar1.Visible = (!Calendar1.Visible);
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        try
        {
          TextBox Txt=(TextBox)this.Parent.FindControl(TargetControlId);
          Txt.Text = string.Format("{0}",Calendar1.SelectedDate.ToString("d",CultureInfo.CreateSpecificCulture("en-GB")));
          Calendar1.Visible = false;
        }
        catch (Exception ex)
        {
            LblErrorMsg.Text = ex.Message;
        }
    }
   
}
